package com.lyf.viewapp.gps;

import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.lyf.viewapp.R;
import com.lyf.viewapp.util.LogUtils;
import com.lyf.viewapp.util.Util;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 定位服务
 *
 * @author lyf
 * @date 2018年6月25日 10:24:00
 */
public class LocationActivity extends AppCompatActivity {

    private Intent intent;
    private NotificationManager manager;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        ButterKnife.bind(this);
        intent = new Intent(this, LocationService.class);
        manager = (NotificationManager)getSystemService(Service.NOTIFICATION_SERVICE);
        Util.ignoreBatteryOptimization(this);
        // bindService(intent,serviceConnectionLocation, Context.BIND_ABOVE_CLIENT);
        // startService(intent);
    }

    @OnClick({R.id.location_start, R.id.location_end})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.location_end:
                // unbindService(serviceConnectionLocation);
                manager.cancel(1001);
                LocationTool.unRegisterLocation();
                break;
            case R.id.location_start:
                manager.notify(1001, Util.buildNotification());
                LocationTool.registerLocation(this, 2000, 5, new LocationTool.OnLocationChangeListener() {
                    @Override
                    public void getLastKnownLocation(Location location) {

                    }

                    @Override
                    public void onLocationChanged(Location location) {
                        LogUtils.showToast(LocationActivity.this, "x:" + location.getLatitude()
                                + "\ny:"+ location.getLongitude());
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {

                    }
                });
                // bindService(intent,serviceConnectionLocation, Context.BIND_ABOVE_CLIENT);
                break;
                default:break;
        }
    }

    /**
     * 在Activity中，我们通过ServiceConnection接口来取得建立连接与连接意外丢失的回调
     */
    ServiceConnection serviceConnectionLocation = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 建立连接
            // 获取服务的操作对象
            LocationService.LocationBinder binder = (LocationService.LocationBinder) service;
            binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 连接断开
        }
    };

    @Override
    protected void onDestroy() {
        if (serviceConnectionLocation!=null) {
            //unbindService(serviceConnectionLocation);
        }
        LocationTool.unRegisterLocation();
        super.onDestroy();
    }
}
